package com.zhinong.controller.admin;

import com.zhinong.dto.FarmPageQueryDTO;
import com.zhinong.entity.Farm;
import com.zhinong.result.PageResult;
import com.zhinong.result.Result;
import com.zhinong.service.FarmService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/farm")
public class FarmController {

    @Autowired
    private FarmService farmService;


    @GetMapping("/page")
    @ApiOperation("农场查询")
    public Result<PageResult> pageQuery(FarmPageQueryDTO farmPageQueryDTO) {
        log.info("pageQuery:{}",farmPageQueryDTO);
        PageResult pageResult = farmService.pageQuery(farmPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询农场")
    public Result<Farm> getById(@PathVariable Long id){
        log.info("根据id查询农场：{}",id);
        Farm farm = farmService.getById(id);
        return Result.success(farm);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除农场")
    public Result deleteById(@PathVariable Long id){
        log.info("根据id删除农场",id);
        farmService.deleteById(id);
        return Result.success();
    }

    @PutMapping("/{id}")
    @ApiOperation("修改农场信息")
    public Result update(@RequestBody Farm farm){
        log.info("修改农场信息",farm);
        farmService.update(farm);
        return Result.success(farm);
    }
}
